package pro.cherish.knowledgedemovalid.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import pro.cherish.knowledgedemovalid.entity.UserEntity;
import pro.cherish.knowledgedemovalid.service.UserService;
import pro.cherish.knowledgedemovalid.vo.TestVO;
import pro.cherish.knowledgedemovalid.vo.TestVO2;

import javax.validation.Valid;

@Api("测试全局异常")
@Controller
public class TestGlobalExceptionController {
    @ApiOperation(value = "处理异常，在controller方法中",notes = "")
    @GetMapping("testGlobalException")
    public void testUser01(@Valid UserEntity userEntity){
        int a=1/0;
        System.out.println(userEntity);
    }

    @Autowired
    private UserService userService;

    @ApiOperation(value = "service @Valid校验，并且抛出全局",notes = "")
    @GetMapping("testservieValid")
    public void testUser01(){
        TestVO testVO = new TestVO();
        testVO.setAge(12);
        userService.testServieValid(testVO);

    }

    @ApiOperation(value = "service @Valid校验，并且抛出全局",notes = "")
    @GetMapping("testservieValid2")
    public void testUser02(){
        TestVO2 testVO2 = new TestVO2();
        testVO2.setAge(12);
        /**
         * fixme 使用hibernate的校验在service中也同样生效，使用自定义校验条件时可以通过自定义校验打注解或者如下使用@AssetTrue
         * @Data
         * public class TestVO {
         *
         *     private String name;
         *
         *     private Integer age;
         *
         *     @AssertTrue
         *     public boolean isValid(){
         *         if(age<18){
         *             throw new RuntimeException("只允许成年人使用");
         *         }
         *         return true;
         *     }
         * }
         */
        userService.testServieValid2(testVO2);

    }
}
